package com.bk.interceptor;

import java.util.ArrayList;
import java.util.HashMap;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

public abstract class CJWInterceptor implements Interceptor {

	/**
	 * 侧栏标题和地址
	 */
	private ArrayList<HashMap<String, String>> slider = new ArrayList<>();

	public HashMap<String, String> addSlider(String title, String link, boolean absloute) {
		HashMap<String, String> map = new HashMap<>();
		map.put("title", title);
		if (absloute) {
			map.put("link", link);
		} else {
			String key = "/" + key() + "/";
			map.put("link", key + link + "/");
		}
		slider.add(map);
		return map;
	}

	public HashMap<String, String> addSlider(String title, String link) {
		return addSlider(title, link, false);
	}

	/**
	 * 当前应用的url前缀 key
	 * 
	 * @return
	 */
	public abstract String key();

	public abstract void generateSlider(Controller controller);

	public void setupAttribute(Controller controller) {
	}

	@Override
	public void intercept(Invocation invocation) {

		Controller controller = invocation.getController();

		try {
			slider = new ArrayList<>();
			generateSlider(controller);

			controller.setAttr("slider", slider);
			controller.setAttr("systemTitle", "systemTitle");
			controller.setAttr("systemSubtitle", "后台管理");
			controller.setAttr("title", "title");
			controller.setAttr("action", "action");
			controller.setAttr("subtitle", "subtitle");
			controller.setAttr("title", "");
			controller.setAttr("subtitle", "");
			controller.setAttr("footer", "Copyright ©2017-2017 蓁奕互联网科技 All Rights Reserved");
			controller.setAttr("infoUrl", "infoUrl");
			controller.setAttr("deleteUrl", "deleteUrl");
			controller.renderText("CommonInterceport");

			setupAttribute(controller);

			invocation.invoke();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
